/*
* @Author: xfy
* @Date:   2017-09-07 10:25:00
* @Last Modified by:   xfy
* @Last Modified time: 2017-09-12 18:53:57
*/
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
require('../../../node_modules/js-audio-recorder/dist/recorder.js');
import Recorder from 'js-audio-recorder';
/*{
        sampleBits: 16,         // 采样位数，支持 8 或 16，默认是16
        sampleRate: 48000,      // 采样率，支持 11025、16000、22050、24000、44100、48000，根据浏览器默认值，我的chrome是48000
        numChannels: 1         // 声道，支持 1 或 2， 默认是1
}*/
var recorder = new Recorder();
var formData = new FormData();

var _supper         = require('util/supper.js');
var _voice          = require('service/voice-service.js');
var Pagination      = require('util/pagination/index.js');

var num = null;
var i = 0;
var $voiceTime = $('.attendance-create-page-wrap .attendance-create-con .voice-time');

//page逻辑部分
var page = {
    data : {
        listParam :{
            idNo        : _supper.getUrlParam('idNo')
        }
    },
    init : function(){
        this.bindEvent();
        this.onLoad();
    },
    onLoad : function(){
        //加载商品信息
        this.loadProductList();
    },
    bindEvent : function(){
        var _this           = this;
        //开始录音
        $(document).on('click', '.start', function(){

            //设置秒表
            recorder.start().then(function() {
                i = 0;
                $voiceTime.html("0.00s");

                num = setInterval(function() {
                    i++;
                    $voiceTime.html(parseFloat(i / 100, 2) + 's');
                }, 10);

            });
        });

        //结束录音
        $(document).on('click', '.over', function(){
            recorder.stop();
            clearInterval(num); 
        });

        //播放录音
        $(document).on('click', '.play', function(){
            recorder.play();
            clearInterval(num); 
        });

        //上传
        $(document).on('click', '.upload', function(){
            var wavBlog         = recorder.getWAVBlob();
            formData.append('idNo', page.data.listParam.idNo);
            formData.append('upload_file', wavBlog);
            //_this.data.listParam.upload_file = new File([wavBlog], 'cache.wav', {lastModified: Date.now()});
            page.registerVoice();
            clearInterval(num); 
        });

        //播放录音
        $(document).on('click', '.download', function(){
            
            recorder.downloadWAV("cache");
            clearInterval(num); 
        });

        //手动上传
        $(document).on('click', '.upload-hand', function(){
            
            var file = $('.upload-form .change')[0].files[0];
            if (file == undefined) {
                _supper.errorTips('请选择文件');
                return;
            }

            formData.append('idNo', page.data.listParam.idNo);
            formData.append('upload_file', file);
            //_this.data.listParam.upload_file = new File([wavBlog], 'cache.wav', {lastModified: Date.now()});
            page.registerVoice();
            clearInterval(num); 
        });

        //手动删除
        $(document).on('click', '.delete-hand', function(){
            page.deleteVoice();
        });
    },
    //验证搜索id时，是否为数字
    validateId : function(data){
        return !$.isNumeric(data);
    },
    //加载所有记录
    loadProductList : function(){
        var _this           = this,
            orderListHtml   = '',
            $voiceCon        = $('.attendance-create-page-wrap .attendance-create-con .voice'),
            listData        = {
                idNo : page.data.listParam.idNo
            };

            _voice.selectVoice(listData, function(res){
                $voiceCon.html('<label class="voice-label"><i class="fa fa-user-circle-o"></i></label>未注册');
            }, function(errMsg){
                $voiceCon.html('<label class="voice-label"><i class="fa fa-user-circle-o"></i></label>已注册');
            });

            
        //$listCon.html('<div class="loading"></div>');
    },
    registerVoice : function(){
        var _this           = this,
            $con            = $('.attendance-create-page-wrap .attendance-create-con .voice-con');
        _voice.registerVoice(formData, function(res){
            _supper.successTips('声纹注册成功');
            window.setTimeout(function() {window.location.href = './attendance-create.html?idNo=' + _this.data.listParam.idNo;}, 1500);
    
        }, function(errMsg){
            _supper.errorTips('声纹注册失败');
        });

        formData.delete(idNo);
    },
    deleteVoice : function(){
        var _this           = this,
            listData        = {
                idNo : page.data.listParam.idNo
            };
        _voice.deleteVoice(listData, function(res){
            _supper.successTips('声纹删除成功');
            window.setTimeout(function() {window.location.href = './attendance-create.html?idNo=' + _this.data.listParam.idNo;}, 1500);
    
        }, function(errMsg){
            _supper.errorTips('声纹删除失败');
        });

        formData.delete(idNo);
    }
};
$(function(){
    page.init();
});